package asteroids.model.programs.expression;

import java.util.ArrayList;

import asteroids.model.programs.type.*;

public abstract class ComposedFunction extends DoubleExpression {

	private E left;
	private E right;
	
	public ComposedFunction(int line, int column, E left, E right) {
		super(line, column);
		this.left =  left;
		this.right = right;
	}
	
	
	@Override
	public abstract DoubleType getValue();
	
	public E getLeft(){
		return left;
	}
	
	public E getRight(){
		return right;
	}
	
	@Override
	public ArrayList<VariableExpression> getVariables(){
		ArrayList<VariableExpression> list = left.getVariables();
		list.addAll(right.getVariables());
		return list;
	}

	@Override
	public String typecheck(){
		String leftTypeCheck = getLeft().typecheck();
		String rightTypeCheck = getRight().typecheck();
		if(!(leftTypeCheck + rightTypeCheck).equals(""))
			return leftTypeCheck + rightTypeCheck;
		if(getLeft().getValue() == null || getRight().getValue() == null)
			return "Typecast error at line " + getLine() + ", column " + getColumn() + ".\n";
		if(getLeft().getValue().getClass() != DoubleType.class || getRight().getValue().getClass() != DoubleType.class)
			return "Typecast error at line " + getLine() + ", column " + getColumn() + ".\n";
		else return "";
	}
}
